package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.Queryteacher;
import com.atguigu.eduservice.service.EduTeacherService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-16
 */
//@CrossOrigin解决跨域问题
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }




    @ApiOperation(value = "所有讲师列表带条件分页")
    @PostMapping("{page}/{limit}")
    public R allteacher(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
           @RequestBody(required = false) Queryteacher queryteacher){
//        //制造异常
//        try{
//            int i=1/0;
//        }catch (Exception e){
//            throw new GuiliException(20001,"Guili异常");
//        }
//        int i=1/0;

        //分页查询
        Page<EduTeacher> pageParam = new Page<>(page, limit);

       QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        //多条件查询--wrapper
        String name = queryteacher.getName();
        Integer level = queryteacher.getLevel();
        String begin = queryteacher.getBegin();
        String end = queryteacher.getEnd();

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
         wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(pageParam, wrapper);
        //集合
        List<EduTeacher> records = pageParam.getRecords();
        //总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);

    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("delteacher/{id}")
    public R delete(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean result = eduTeacherService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addteacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        eduTeacherService.save(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("select/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        return R.ok().data("item", eduTeacher);
    }


    @ApiOperation(value = "根据id修改讲师")
    //缓存失效，同步更新
    @CacheEvict(value = "teacher",key = "'selectteacherList'")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher
    ) {
        eduTeacher.setId(id);
        eduTeacherService.updateById(eduTeacher);
        return R.ok();
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("update")
    public R updateById(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher
    ) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag==true){

            return R.ok();
        }
        return R.error();
    }

}

